package com.wxy.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.common.core.domain.AjaxResult;
import com.wxy.common.utils.DozerUtil;
import com.wxy.common.utils.ServletUtils;
import com.wxy.common.utils.StringUtils;
import com.wxy.web.dto.RoleDto;
import com.wxy.web.dto.UserRoleDto;
import com.wxy.web.model.Role;
import com.wxy.web.service.IPermissionService;
import com.wxy.web.service.IRoleService;
import com.wxy.web.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2021-06-13
 */
@RestController
@RequestMapping("/admin/role")
public class RoleController {
    @Autowired
    public DozerUtil dozerUtil;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IUserService userService;

    @GetMapping("list")
    public AjaxResult list(Role role)
    {
        Integer pageNum = ServletUtils.getParameterToInt("page",1);
        Integer pageSize = ServletUtils.getParameterToInt("limit",10);

        LambdaQueryWrapper<Role> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotEmpty(role.getRolename()))
            lambdaQueryWrapper.like(Role::getRolename , role.getRolename());
        if(StringUtils.isNotEmpty(role.getBeizhu()))
            lambdaQueryWrapper.like(Role::getBeizhu , role.getBeizhu());

        Page<Role> page = new Page<>(pageNum , pageSize);
        page.addOrder(OrderItem.desc("id"));
        IPage<Role> ipage = roleService.page(page, lambdaQueryWrapper);
        HashMap<String,Object> map = new HashMap<>();
        map.put("list", ipage.getRecords());
        map.put("count", ipage.getTotal());
        AjaxResult result = AjaxResult.success();
        result.put(AjaxResult.DATA_TAG,map);
        return result;
    }

    @GetMapping("info")
    public AjaxResult info(Integer id)
    {
        Role role = roleService.getById(id);
        RoleDto dto = dozerUtil.map(role, RoleDto.class);
        dto.setPermissionIds(permissionService.listPermissionIdsByRoleId(id));
        AjaxResult result = AjaxResult.success();
        result.put(AjaxResult.DATA_TAG,dto);
        return result;
    }

    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody RoleDto roleDto)
    {
        Role role = dozerUtil.map(roleDto, Role.class);
        roleService.save(role);
        for (Integer permissionId : roleDto.getPermissionIds()) {
            roleService.insertRolePermission(role.getId(),permissionId);
        }
        AjaxResult result = AjaxResult.success();
        return result;
    }

    @PostMapping("edit")
    public AjaxResult edit(@Validated @RequestBody RoleDto roleDto)
    {
        Role role = dozerUtil.map(roleDto, Role.class);
        roleService.updateById(role);
        roleService.deleteRolePermissionByRoleId(roleDto.getId());
        for (Integer permissionId : roleDto.getPermissionIds()) {
            roleService.insertRolePermission(roleDto.getId(),permissionId);
        }
        AjaxResult result = AjaxResult.success();
        return result;
    }

    @PostMapping("del/{id}")
    public AjaxResult del(@PathVariable Integer id)
    {
        roleService.deleteRolePermissionByRoleId(id);
        roleService.removeById(id);
        AjaxResult result = AjaxResult.success();
        return result;
    }

    @GetMapping("user")
    public AjaxResult user()
    {
        Integer pageNum = ServletUtils.getParameterToInt("page",1);
        Integer pageSize = ServletUtils.getParameterToInt("limit",10);
        Integer roleId = ServletUtils.getParameterToInt("roleId");

        HashMap<String,Object> map = new HashMap<>();
        map.put("list", userService.listByRoleId(roleId,pageNum,pageSize));
        map.put("count", userService.countByRoleId(roleId));

        AjaxResult result = AjaxResult.success();
        result.put(AjaxResult.DATA_TAG,map);

        return result;
    }

    @PostMapping("userRemove")
    public AjaxResult userRemove(@Validated @RequestBody UserRoleDto userRoleDto)
    {
        int result = roleService.deleteRoleUser(userRoleDto.getRoleId(),userRoleDto.getUserId());

        AjaxResult ajaxResult = null;
        if(result == 1)
            ajaxResult = AjaxResult.success();
        else
            ajaxResult = AjaxResult.error();
        return ajaxResult;
    }
}
